package leeon.bbsbrower;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Map;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The main plugin class to be used in the desktop.
 */
public class BBSBrowerPlugin extends AbstractUIPlugin {

	//The shared instance.
	private static BBSBrowerPlugin plugin;
	
	/**
	 * The constructor.
	 */
	public BBSBrowerPlugin() {
		plugin = this;
	}

	/**
	 * This method is called upon plug-in activation
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
	}

	/**
	 * This method is called when the plug-in is stopped
	 */
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		plugin = null;
	}

	/**
	 * Returns the shared instance.
	 */
	public static BBSBrowerPlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path.
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return AbstractUIPlugin.imageDescriptorFromPlugin("bbsbrower", path);
	}
	
	/**
	 * 根据传入的插件相对路径，获取文件的file对象。
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static File getRealFile(String path) throws IOException {
		//URL url = getDefault().find(new Path(path));
		//String filePath = Platform.asLocalURL(url).getFile();
		
		URL url = FileLocator.find(getDefault().getBundle(), new Path(path), (Map<?, ?>)null);
		String filePath = FileLocator.toFileURL(url).getFile();
		File file = new File(filePath);
		return file;
	}
}
